Skip to content

[Backend] Implement Winston Request/Response Logger#322

Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/277-winston-logger
Open

[Backend] Implement Winston Request/Response Logger#322
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/277-winston-logger

Conversation

@armorbreak001
Copy link
Copy Markdown

Fixes #277

What was done

  • Enhanced winston.logger.ts:
    • Console transport with colored, human-readable output for development
    • JSON-formatted file transports for production (in /logs/ directory)
    • Daily rotating files via winston-daily-rotate-fileapplication-YYYY-MM-DD.log and error-YYYY-MM-DD.log
    • Max file size 20MB, auto-cleanup after 14-30 days
  • New logger.middleware.ts:
    • Global NestJS middleware intercepting every incoming HTTP request
    • Logs format: [METHOD] [URL] - [IP] - [StatusCode] - [Elapsed MS]
    • Auto log level selection: error for 5xx, warn for 4xx, info for success
    • Redacts sensitive fields from request body: password, token, secret, apiKey, authorization, bearer, accessToken, refreshToken
    • IP sanitization (localhost alias, IPv6 mapping cleanup)
    • Skips logging request bodies larger than 10KB to prevent log bloat
  • Updated LoggerModule: Now implements NestModule, applies middleware globally via configure()
  • Added winston-daily-rotate-file dependency

How to verify

  1. cd backend && npm install (to install winston-daily-rotate-file)
  2. npm run start:dev
  3. Make any HTTP request (e.g., GET /health)
  4. Check console output: should see formatted log line like:
    2026-04-14 19:30:00 info: GET /health - localhost - 200 - 5ms
  5. Set NODE_ENV=production and restart — logs will write to logs/application-YYYY-MM-DD.log and logs/error-YYYY-MM-DD.log
  6. Send a POST with {"password": "secret", "token": "abc"} — verify it logs as {"password": "[REDACTED]", "token": "[REDACTED]"}

- Add LoggerMiddleware intercepting all HTTP requests
- Logs: [Method] [URL] [IP] [StatusCode] [DurationMs]
- Redacts sensitive fields: password, token, apiKey, secret, etc.
- Respects X-Forwarded-For for reverse proxy deployments
- Uses WinstonLogger with level based on status code (error/warn/info)
- Logs request body (redacted) for non-GET requests
- Includes content-length and user-agent in log output
- Applied globally via NestModule.configure() for all routes
@armorbreak001 armorbreak001 force-pushed the bounty/277-winston-logger branch from 2b16b70 to f784d23 Compare April 14, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Implement Winston Request/Response Logger

1 participant